using System;

namespace SharpMud.Text.Escaping
{
	/// <summary>
	/// Provides functionality for escaping text/data by converting the text into
	/// the hexadecimal representation of its binary data, and back again.
	/// </summary>
	public class Hex
	{
		private Hex()
		{
		}

		public static string Escape(string text)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			foreach(char c in text.ToCharArray())
			{
				sb.Append(System.Uri.HexEscape(c));
			}
			return sb.ToString();
		}

		public static string Unescape(string text)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			int x=0;
			for(x=0;x<text.Length;x++)
			{
				if(System.Uri.IsHexEncoding(text,x))
				{
					sb.Append(System.Uri.HexUnescape(text,ref x));
				}
				else
				{
					sb.Append(text[x]);
				}
			}
			return sb.ToString();
		}
	}
}
